package com.mall.product.service.impl;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.common.exception.BadRequestException;
import com.common.exception.BizIllegalException;
import com.mall.product.entity.AttrEntity;
import com.mall.product.service.AttrService;
import com.mall.product.vo.AttrGroupWithAttrVo;
import com.mall.product.vo.SpuItemGroupAttrVo;
import com.mall.product.vo.SpuItemVo;
import org.apache.commons.lang.ObjectUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.common.utils.PageUtils;
import com.common.utils.Query;

import com.mall.product.dao.AttrGroupDao;
import com.mall.product.entity.AttrGroupEntity;
import com.mall.product.service.AttrGroupService;
import org.springframework.validation.BindException;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {
    @Autowired
    private AttrService attrService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 进行检索查询
     * @param params
     * @param cateLogId
     * @return
     */
    @Override
    public PageUtils queryPage(Map<String, Object> params, Long cateLogId) {
        String key = (String) params.get("key");
        QueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(key)){
            wrapper.and((obj ->{
                obj.eq("attr_group_id",key)
                        .or().like("attr_group_name",key);
            }));
        }
        if (cateLogId == 0){
            IPage<AttrGroupEntity> page = this.page(
                    new Query<AttrGroupEntity>().getPage(params),wrapper);
                return new PageUtils(page);
        }else {
            wrapper.eq("catelog_id",cateLogId);
            IPage<AttrGroupEntity> page = this.page(
                    new Query<AttrGroupEntity>().getPage(params),wrapper);
            return new PageUtils(page);
        }
    }

    /**
     * 防止重复添加
     * @param attrGroup
     */
    @Override
    public void saveNotRepeat(AttrGroupEntity attrGroup) {
        if (attrGroup == null){
            throw new BizIllegalException("参数不合法");
        }
        AttrGroupEntity entity = this.lambdaQuery()
                .setEntity(attrGroup)
                .one();
        if (entity == null){
            this.save(attrGroup);
        }
        else {
            throw new BizIllegalException("禁止重复添加");
        }
    }

    /**
     * 根据3级分类编号查询对应的所有的属性组
     * @param catelogfId
     * @return
     */
    @Override
    public List<AttrGroupWithAttrVo> getAttrgroupWithAttrsByCatelogId(Long catelogfId) {
        List<AttrGroupEntity> attrGroupEntities = this.lambdaQuery()
                .eq(AttrGroupEntity::getCatelogId, catelogfId)
                .list();
        List<AttrGroupWithAttrVo> list = attrGroupEntities.stream().map((m) -> {
            AttrGroupWithAttrVo vo = new AttrGroupWithAttrVo();
            BeanUtils.copyProperties(m, vo);
            List<AttrEntity> relationAttr = attrService.attrRelationAttr(m.getAttrGroupId());
            vo.setAttrs(relationAttr);
            return vo;
        }).collect(Collectors.toList());
        return list;
    }

    @Override
    public List<SpuItemGroupAttrVo> getAttrgroupWithSpuId(Long spuId, Long catalogId) {
        List<SpuItemGroupAttrVo> spuItemGroupAttrVos = baseMapper.getAttrgroupWithSpuId(spuId,catalogId);
        return spuItemGroupAttrVos;
    }
}